/**
 *	平台管理
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Controller.api;


import com.framerock.server.Response;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framerock.baselib.Xinhu;
import com.framework.Model.ModelOption;

import java.util.ArrayList;
import java.util.Map;


public class ControllerPlat extends ControllerApi {


    protected String runAction(String action)
    {
        if(mRequest.USERTYPE==0)return Response.returnError("not auth");
        if(action.equals("cogsave"))return cogsave();
        if(action.equals("coginfo"))return coginfo();
        return super.runAction(action);
    }

    //保存系统设置
    private String cogsave()
    {
        String systemtitle  = mRequest.post("systemtitle");
        String rockfile_url = mRequest.post("rockfile_url");
        String rockfile_key = mRequest.post("rockfile_key");
        ModelOption option = new ModelOption(this.nDb);
        option.setVal("systemtitle", systemtitle);
        option.setVal("rockfile_url", rockfile_url);
        option.setVal("rockfile_key", rockfile_key);
        String str;
        str = mRequest.post("xinhukey");
        if(!Rock.contain(str, "*"))option.setVal("xinhukey", str);

        str = mRequest.post("smstype");
        option.setVal("smstype", str);
        str = mRequest.post("smsqianm");
        option.setVal("smsqianm", str);
        str = mRequest.post("findcode");
        option.setVal("findcode", str);
 
        str = mRequest.post("yunpiankey");
        if(!Rock.contain(str, "*"))option.setVal("yunpiankey", str);

        str = mRequest.post("service_url");
        option.setVal("service_url", str);
        str = mRequest.post("service_push");
        option.setVal("service_push", str);
        str = mRequest.post("service_recid");
        option.setVal("service_recid", str);
        str = mRequest.post("service_cehui");
        option.setVal("service_cehui", str);

        Xinhu.initVal();
        return Response.returnSuccess("OK");
    }

    //读取配置
    private String coginfo()
    {
        Json json = new Json();
        ArrayList<Map<String, String>> rows = ModelOption.get(this.nDb).getAll("`num` IN('systemtitle','rockfile_url','rockfile_key','xinhukey','smstype','smsqianm','yunpiankey','findcode','service_url','service_push','service_recid','service_cehui')", "num,value");
        Map<String, String> rs;
        String num,val;
        if(rows!=null)for(int i=0; i<rows.size(); i++){
            rs = rows.get(i);
            num= rs.get("num");
            val= rs.get("value");
            if((num.equals("xinhukey") || num.equals("yunpiankey")) && !Rock.isEmpt(val))val = val.substring(0, 5)+"****";
            json.putVal(num, val);
        }
        return Response.returnSuccess(json.getJson());
    }
}
